<template>
  <div class="page-content">

    <MenuBar v-if="!isMobile" passTab="-1" />



    <PhoneMenuBar v-else passTab="-1" />



    <div style="height: 70px;"></div>

    <SwiperBanner :isMobile="isMobile" />

    <!-- 最新产品 -->
    <div class="title-show">

      <div class="title-inner">
        <div style="display: flex;align-items: center;">
          <SvgIcon color="#000" name="hot_p" :width="isMobile ? '40px' : '52px'" :height="isMobile ? '40px' : '52px'">
          </SvgIcon>
          <span class="title-en">NEW ITEMS</span>
          <span class="title-ch">最新产品</span>
        </div>

      </div>

      <div class="swiper-container">
        <swiper v-if="n_productList.length > 0" :modules="modules"
          :autoplay="{ delay: 3000, disableOnInteraction: false }" :loop="true" :space-between="isMobile ? 10 : 20"
          :slides-per-view="isMobile ? 2 : 5" :centered-slides="true" style="height: 100%;">

          <swiper-slide v-for="(item) in n_productList" :key="item.id" class="custom-slide">

            <div class="news-item" @click="openProductInfoPage(item.id)">

              <div class="product-image">
                <img :src="item.imgurl" class="item-img" />
              </div>


              <div class="item-content">
                <span class="item-label">{{ item.prop_list[0]["value"] || '未知' }}</span>
                <span class="item-no">货号{{ item.prop_list[3]["value"] || '未知' }}</span>
                <span class="item-name">{{ item.title }}</span>
                <span class="item-time">发售日期:{{ item.addtime.length > 10 ? item.addtime.substring(0, 10) : item.addtime
                  }}</span>

                <div class="item-money">{{ item.prop_list[2]["value"] || '-' }}円</div>
              </div>

            </div>
          </swiper-slide>
        </swiper>
      </div>

    </div>
    <!-- 最新产品 -->


    <!-- 产品中心 -->
    <div class="title-show">

      <div class="title-inner">
        <div style="display: flex;align-items: center;">
          <SvgIcon color="#000" name="product" :width="isMobile ? '40px' : '52px'" :height="isMobile ? '40px' : '52px'">
          </SvgIcon>
          <span class="title-en">PRODUCT LINKS</span>
          <span class="title-ch">产品中心</span>
        </div>
        <div @click="checkProductListPage()" class="custom-button">查看列表 <SvgIcon name="arrow_right" color="3A7BBF"
            style="margin-left: 2px;"></SvgIcon>
        </div>
      </div>

      <div class="product-list">
        <div class="product-item" :style="{ 'background-color': typeId == item.id ? '#787575' : '#5a5757' }"
          @click="chooseCategory(item.id)" v-for="item in c_productList" :key="item.id">{{
            item.name }}</div>
        <!-- <div class="product-item">遥控模型</div>
        <div class="product-item">迷你四驱车</div>
        <div class="product-item">伊达雷利</div>
        <div class="product-item">万代BanDai</div>
        <div class="product-item">工具Tool</div>
        <div class="product-item">油漆&喷漆</div> -->
      </div>


      <div class="toy-layout">


        <div class="toy-list">
          <div class="toy-item" v-for="(item) in all_productList" :key="item.id" @click="openProductInfoPage(item.id)">

            <div class="product-image">
              <img :src="item.imgurl" class="toy-img" />
            </div>


            <div class="toy-content">
              <span class="toy-label">{{ item.prop_list[0]["value"] || '未知' }}</span>
              <span class="toy-name">{{ item.title }}</span>
              <div class="toy-bottom">
                <span class="toy-no">货号{{ item.prop_list[3]["value"] || '-' }}</span>
                <span class="toy-money">{{ item.prop_list[2]["value"] || '-' }}円</span>
              </div>

            </div>

          </div>
        </div>
      </div>

    </div>
    <!-- 产品中心 -->


    <!-- 咨询中心 -->
    <div class="title-show">

      <div class="title-inner">
        <div style="display: flex;align-items: center;">
          <SvgIcon color="#000" name="message" :width="isMobile ? '40px' : '52px'" :height="isMobile ? '40px' : '52px'">
          </SvgIcon>
          <span class="title-en">PRODUCT LINKS</span>
          <span class="title-ch">资讯中心</span>
        </div>
        <div @click="checkNewsListPage()" class="custom-button">查看列表 <SvgIcon name="arrow_right" color="3A7BBF"
            style="margin-left: 2px;"></SvgIcon>
        </div>
      </div>

      <div class="message-layout">

        <n-list size="large" hoverable clickable>

          <n-list-item v-for="item in all_newsList" v-bind:key="item.id" @click="openNewDetailPage(item)">
            <n-thing content-style="margin-top: 10px;">
              <template #header>
                <h3 style="font-size: 18px; margin: 0; font-weight: bold;">{{ item.title }}</h3>
              </template>
              <template #description>
                <n-space size="large" style="margin-top: 4px">
                  <n-tag :bordered="false" type="info" size="large">
                    {{ item.addtime.length > 10 ? item.addtime.substring(0, 10) : item.addtime }}
                  </n-tag>
                </n-space>
              </template>
              <div class="message-content">{{ item.description }}</div>
            </n-thing>
          </n-list-item>



        </n-list>
      </div>
    </div>
    <!-- 咨询中心 -->
    <BottomBar />
  </div>
</template>

<script setup name="homePage">
import { useRouter, useRoute } from 'vue-router'
import { API_URL } from '@/config'
import { newProductList, productCategory, productList, newsList } from '@/api'
import { ref, onMounted } from 'vue';
import SwiperBanner from '@/components/SwiperBanner.vue'
import MenuBar from '@/components/MenuBar.vue'
import PhoneMenuBar from '@/components/PhoneMenuBar.vue'
import BottomBar from '@/components/BottomBar.vue'
import SvgIcon from '@/components/SvgIcon.vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Autoplay } from 'swiper/modules';
// Swiper 模块
const modules = [Navigation, Autoplay];
// 创建一个响应式变量来存储是否为移动设备
const isMobile = ref(false);

// 定义一个函数来检查用户代理
const checkDevice = () => {
  const userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // 设置isMobile的值
  isMobile.value = /android/i.test(userAgent) || /iPhone|iPad|iPod/i.test(userAgent);

  console.log("home-isMobile",isMobile.value)
};

const n_productList = ref([])
const getNewProductList = async () => {
  const params = {
    new_ok: 1,
    page: 1,
    size: 99
  }
  const result = await newProductList(params)
  const list = result.data.map(item => {
    return {
      ...item,
      imgurl: item.imgurl?.startsWith('../') ? API_URL + item.imgurl.substring(2) : item.imgurl
    }
  })
  if (list.length > 10) {
    n_productList.value = list.slice(0, 10);
  } else {
    n_productList.value = list
  }

}

const typeId = ref(-1)
const c_productList = ref([])
const getProductCategory = async () => {
  const result = await productCategory()
  c_productList.value = result.data;

  typeId.value = c_productList.value[0].id

  getProductList();
}


const all_productList = ref([])
const getProductList = async () => {
  const params = {
    type_id: typeId.value,
    page: 1,
    size: 12
  }
  const result = await productList(params)
  const list = result.data.map(item => {
    return {
      ...item,
      imgurl: item.imgurl?.startsWith('../') ? API_URL + item.imgurl.substring(2) : item.imgurl
    }
  })
  all_productList.value = list
}

const chooseCategory = async (categoryId) => {
  typeId.value = categoryId;

  getProductList();
}

const all_newsList = ref([])
const getNewsList = async () => {
  const params = {
    page: 1,
    size: 3,
    navbar_id: 4
  }
  const result = await newsList(params)

  all_newsList.value = result.data
}


onMounted(() => {
  checkDevice();
  getNewProductList();
  getProductCategory();
  getNewsList();

});




const router = useRouter()
const checkProductListPage = () => {
  router.push({
    name: 'Product',
    query: {
      typeId: "-1"
    }
  })
}
const checkNewsListPage = () => {
  router.push({
    name: 'News',
    query: {
      typeId: "-1"
    }
  })
}
const openProductInfoPage = (pId) => {
  router.push({
    name: 'PInfo',
    query: {
      productId: pId
    }
  })
}

const openNewDetailPage = (item) => {
  router.push({
    name: 'NewsDetail',
    query: {
      advanceId: item.id
    }
  })


}

</script>

<style scoped lang="scss">
.page-content {
  width: 100vw;
  height: 100%;
  background-color: #CDD1D3;
  display: flex;
  flex-direction: column;


  /* 手机端样式 */
  @media (max-width: 768px) {

    .title-show {
      width: 100%;
      margin-top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }

    .title-inner {
      width: 95%;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .title-en {
      font-size: 18px;
      color: #000;
      font-weight: 600;
      margin-left: 2px;
    }

    .title-ch {
      font-size: 15px;
      color: #000;
      font-weight: 500;
      margin-left: 2px;
    }

    .custom-button {
      margin-right: 5px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      width: 66px;
      height: 30px;
      border: 1px solid #3A7BBF;
      background-color: white;
      color: #3A7BBF;
      font-size: 12px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    .message-layout {
      margin: 15px;
      width: 94%;

      .message-content {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: normal;
        /* 可省略，-webkit-line-clamp 会处理 */
        word-break: break-word;
        max-width: 100%;
      }
    }

    .product-list {
      margin-top: 15px;
      width: 94%;
      display: flex;
      flex-wrap: wrap;
      gap: 5px;

      .product-item {
        font-weight: 700;
        padding: 9px;
        text-align: center;
        color: white;
        background-color: #5a5757;
        transition: background-color 0.3s ease;
        /* 平滑过渡效果 */
        cursor: pointer;
      }

      .product-item:hover {
        background-color: #787575;
      }
    }

    .toy-layout {
      padding-top: 15px;
      margin: 0 auto;
      width: 94%;
      box-sizing: border-box;
      /* 确保padding不影响总宽度 */

      .toy-list {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
      }

      .toy-item {
        flex: 0 0 calc(50% - 8px * 3/4);
        /* 移动端默认占满一行 */
        display: flex;
        flex-direction: column;
        padding: 10px;
        background: white;
        box-sizing: border-box;
        cursor: pointer;

        .toy-img {
          object-fit: contain;
          height: 120px;
          // max-height: 192px;
          /* 新增：确保图片适应容器宽度 */
          width: 100%;
        }

        .toy-content {
          display: flex;
          flex-direction: column;
          padding-top: 10px;

          .toy-label {
            margin-bottom: 7px;
            border: 1px #3A7BBF solid;
            color: #3A7BBF;
            font-size: 10px;
            width: fit-content;
            padding: 0px 20px;
          }


          .toy-name {
            font-weight: 500;
            color: black;
            font-size: 13px;
            line-height: 24px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: normal;
            /* 可省略，-webkit-line-clamp 会处理 */
            word-break: break-word;
            max-width: 100%;
            flex: 1;
          }

          .toy-bottom {
            margin-top: 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;



            .toy-no {
              color: #5e7387;
              font-size: 13px;

            }

            .toy-money {
              color: #5e7387;
              font-size: 13px;
            }

          }


        }
      }
    }
  }

  @media (min-width: 769px) {

    .title-show {
      width: 100%;
      margin-top: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }


    .title-inner {
      width: 80%;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .title-en {
      font-size: 28px;
      color: #000;
      font-weight: 600;
      margin-left: 5px;
    }

    .title-ch {
      font-size: 18px;
      color: #000;
      font-weight: 500;
      margin-left: 15px;
    }

    .custom-button {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 15px;
      width: 90px;
      height: 30px;
      border: 2px solid #3A7BBF;
      background-color: white;
      color: #3A7BBF;
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    .message-layout {
      margin-top: 15px;
      width: 80%;

      .message-content {
        display: -webkit-box;
        -webkit-line-clamp: 1;
        line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: normal;
        /* 可省略，-webkit-line-clamp 会处理 */
        word-break: break-word;
        max-width: 100%;
      }
    }

    .product-list {
      margin-top: 15px;
      width: 80%;
      display: flex;
      gap: 5px;

      .product-item {
        font-weight: 700;
        padding: 5px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        background-color: #5a5757;
        transition: background-color 0.3s ease;
        /* 平滑过渡效果 */
        cursor: pointer;
      }

      .product-item:hover {
        background-color: #787575;
      }
    }


    .toy-layout {
      padding-top: 15px;
      margin: 0 auto;
      width: 80%;
      box-sizing: border-box;
      /* 确保padding不影响总宽度 */

      .toy-list {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
      }

      .toy-item {
        flex: 0 0 calc(25% - 8px * 3/4);
        /* 移动端默认占满一行 */
        display: flex;
        flex-direction: column;
        padding: 20px 15px;
        background: white;
        box-sizing: border-box;
        cursor: pointer;

        .toy-img {
          object-fit: cover;
          height: 192px;
          /* 新增：确保图片适应容器宽度 */
          width: 100%;
        }

        .toy-content {
          flex: 1;
          display: flex;
          flex-direction: column;
          padding-top: 10px;
          width: 100%;

          .toy-label {
            margin-bottom: 7px;
            border: 1px #3A7BBF solid;
            color: #3A7BBF;
            font-size: 10px;
            width: fit-content;
            padding: 0px 20px;
          }



          .toy-name {
            font-weight: 500;
            color: black;
            font-size: 13px;
            line-height: 24px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: normal;
            /* 可省略，-webkit-line-clamp 会处理 */
            word-break: break-word;
            max-width: 100%;
            flex: 1;
          }

          .toy-bottom {
            margin-top: 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;

            .toy-no {
              color: #5e7387;
              font-size: 13px;
            }

            .toy-money {
              color: #5e7387;
              font-size: 13px;
            }
          }


        }
      }
    }



  }

}




.swiper-slide {
  cursor: pointer;
  width: 250px;
  height: 340px;
}

.swiper-container {
  margin-top: 15px;
  width: 100%;


  .custom-slide {
    width: 100%;
    height: 100%;
    background-color: white;

    .news-item {
      display: flex;
      flex-direction: column;
      padding: 15px;
      // width: 250px;
      width: auto;


      .item-img {
        object-fit: cover;
        height: 192px;
        width: 100%;
      }

      .item-content {
        display: flex;
        flex-direction: column;
        padding-top: 10px;


        .item-label {
          margin-bottom: 7px;
          border: 1px #3A7BBF solid;
          color: #3A7BBF;
          font-size: 10px;
          width: fit-content;
          padding: 0px 20px;
        }

        .item-name {
          font-weight: 500;
          color: black;
          font-size: 13px;
          line-height: 24px;
          display: -webkit-box;
          -webkit-line-clamp: 1;
          line-clamp: 1;
          -webkit-box-orient: vertical;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: normal;
          /* 可省略，-webkit-line-clamp 会处理 */
          word-break: break-word;
          max-width: 100%;
        }

        .item-no {
          color: #5e7387;
          font-size: 13px;
        }

        .item-time {
          color: #777575;
          font-size: 13px;
        }

        .item-money {
          text-align: end;
          color: #111111;
          font-size: 13px;
        }
      }

    }
  }

}

// /* 手机端样式 */
// @media (max-width: 768px) {


//   .height-divider {
//     background-color: white;
//     width: 100%;
//     height: 5px;
//     margin-bottom: 10px;
//   }

//   .logo-layout {
//     width: 100%;
//     display: flex;
//     align-items: center;
//     margin-top: 30px;
//     background-color: white;
//     height: 80px;
//   }
// }


// @media (min-width: 769px) {

//   .height-divider {
//     background-color: white;
//     width: 100%;
//     height: 10px;
//     margin-bottom: 10px;
//   }

//   .logo-layout {
//     width: 100%;
//     display: flex;
//     align-items: center;
//     margin-top: 30px;
//     background-color: white;
//     height: 90px;
//   }
// }

// .bottom-layout {
//   padding: 15px;
//   background-color: #3C3C3C;
//   min-height: 72px;
//   display: flex;
//   flex-direction: column;
//   align-items: center;
//   justify-content: center;
// }</style>